﻿using System;
using System.Collections.Generic;
using System.Linq;
using BusinessLogic.BusinessEntity.Entities;
using BusinessLogic.Managers.Security;

namespace BusinessLogic.Managers.Security
{
	public static class SecurityManager
	{
		public static bool CheckWhetherAccessIsGranted(Type protectedType, IEnumerable<Role> roles)
		{
			var attributes = (GrantAccessAttribute[]) protectedType.GetCustomAttributes(typeof (GrantAccessAttribute), false);

			if(attributes != null && attributes.Length > 0)
			{
				IEnumerable<Role> rolesRequired = attributes.Select(attribute => attribute.Role);

				foreach (var role in rolesRequired)
				{
					if (roles.Contains(role))
					{
						return true;
					}
				}
			}

			return false;
		}
	}
}